import 'dart:convert';

import 'package:dio/dio.dart';
import 'dart:developer' as developer;

/// 详细的日志拦截器，输出请求和响应日志
class HttpInterceptor extends Interceptor {
  @override
  void onRequest(RequestOptions options, RequestInterceptorHandler handler) {
    // developer.log(
    //   '🌐 [REQUEST] ${options.method} ${options.uri}\n'
    //   '📋 Headers: ${options.headers}\n'
    //   '📦 Query Parameters: ${options.queryParameters}\n'
    //   '📄 Request Data: ${options.data}\n'
    //   '⏱️  Time: ${DateTime.now()}',
    //   name: 'HttpInterceptor_onRequest',
    // );

    developer.log(''' 请求头URL: ${options.uri}
                      请求Header: ${options.headers}
                      ⏱️开始Time: ${DateTime.now()},
                      ''', name: '请求开始🚄');
    super.onRequest(options, handler);
  }

  @override
  void onResponse(Response response, ResponseInterceptorHandler handler) {
    // developer.log(
    //   '✅ [RESPONSE] ${response.statusCode} ${response.requestOptions.uri}\n'
    //   '📋 Response Headers: ${response.headers}\n'
    //   '📄 Response Data: ${response.data}\n'
    //   '⏱️  Time: ${DateTime.now()}',
    //   name: 'HttpInterceptor_onResponse',
    // );

    developer.log(''' 请求URL: ${response.requestOptions.uri}
              请求方法: ${response.requestOptions.method}
              请求头: ${response.requestOptions.headers}
              请求参数: queryParameters:${response.requestOptions.queryParameters}   data:${response.requestOptions.data} 
              响应数据🚀🚀: ${jsonEncode(response.data)}
              ⏱️完成Time: ${DateTime.now()}
              ''', name: '请求响应😄');
    super.onResponse(response, handler);
  }

  @override
  void onError(DioException err, ErrorInterceptorHandler handler) {
    developer.log(
      '[ERROR] ${err.requestOptions.method} ${err.requestOptions.uri}\n'
      '错误类型: ${err.type}\n'
      '错误信息: ${err.message}\n'
      '错误数据: ${err.response?.data}\n'
      'Time: ${DateTime.now()}',
      name: '请求错误🙅',
      error: err,
    );
    super.onError(err, handler);
  }
}
